package cn.itsource.interceptor;

import cn.itsource.service.impl.UserServiceImpl;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    //在Controller执行之前执行
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");
        if(token != null){
            Object obj = UserServiceImpl.map.get(token);
            if(obj != null){
                return true;
            }
        }

        //要跳转到登陆页面： 后端项目没有页面 - 给前端一个信息让它去判断跳转
        response.setContentType("application/json");
        response.getWriter().println("{\"code\":401,\"success\":false}");
        return false;//拦截
    }
}
